<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="日志编号" prop="smsLogId">
        <el-input
          v-model="queryParams.smsLogId"
          placeholder="请输入日志编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="短信渠道" prop="channelId">
        <el-select
          v-model="queryParams.channelId"
          placeholder="请选择短信渠道编号"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="channel in channelList"
            :key="channel.id"
            :value="channel.id"
            :label="
              channel.signature +
              `【${getDictLabel(DICT_TYPE.system_sms_channel_type, channel.code)}】`
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板编码" prop="templateCode">
        <el-input
          v-model="queryParams.templateCode"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="短信类型" prop="templateType">
        <el-select
          v-model="queryParams.templateType"
          placeholder="请选择短信类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in system_sms_business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送状态" prop="sendStatus">
        <el-select
          v-model="queryParams.sendStatus"
          placeholder="请选择发送状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in system_sms_send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间" prop="sendTime">
        <el-date-picker
          v-model="queryParams.sendTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="接收状态" prop="receiveStatus">
        <el-select
          v-model="queryParams.receiveStatus"
          placeholder="请选择接收状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in system_sms_receive_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="接收时间" prop="receiveTime">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.receiveTime"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" width="80">
        <template #default="scope">
          {{ (queryParams.pageNo - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="mobile" width="120">
        <template #default="scope">
          <div>
            <div>{{ scope.row.mobile }}</div>
            <el-tooltip v-if="scope.row.userType" :content="scope.row.userId">
              <dict-tag :options="common_user_type" :value="scope.row.userType" />
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="短信内容" prop="smsContent" />
      <el-table-column label="短信类型" prop="templateType" width="100">
        <template #default="scope">
          <dict-tag :options="system_sms_business_type" :value="scope.row.templateType" />
        </template>
      </el-table-column>
      <el-table-column label="短信模板" prop="templateCode" width="200">
        <template #default="scope">
          <div>
            <div>{{ scope.row.templateName }}</div>
            <div>{{ scope.row.templateCode }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="短信渠道" prop="channelCode" width="120">
        <template #default="scope">
          <div>
            <div>{{ scope.row.channelSignature }}</div>
            <dict-tag :options="system_sms_channel_type" :value="scope.row.channelCode" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="发送状态" prop="sendStatus" width="180px">
        <template #default="scope">
          <div>
            <dict-tag :options="system_sms_send_status" :value="scope.row.sendStatus" />
            <div v-if="scope.row.sendTime">{{ scope.row.sendTime }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="接收状态" prop="receiveStatus" width="180px">
        <template #default="scope">
          <div>
            <dict-tag :options="system_sms_receive_status" :value="scope.row.receiveStatus" />
            <div v-if="scope.row.receiveTime">{{ scope.row.receiveTime }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="80px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['system:sms-log:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：详情 -->
  <SmsLogDetail ref="detailRef" />
</template>

<script setup lang="ts">
import { SmsLogApi, SmsLogVO } from '@/api/system/sms/sms_log'
import { SmsChannelApi, SmsChannelVO } from '@/api/system/sms/sms_channel'
import SmsLogDetail from './SmsLogDetail.vue'
import { DICT_TYPE, getDictLabel } from '@/utils/dict'

/** 短信日志 列表 */
defineOptions({ name: 'SystemSmsLog' })

// const message = useMessage() // 消息弹窗
// const { t } = useI18n() // 国际化

const { common_user_type, system_sms_business_type, system_sms_channel_type } = useDict(
  DICT_TYPE.common_user_type,
  DICT_TYPE.system_sms_business_type,
  DICT_TYPE.system_sms_channel_type
)
const { system_sms_send_status, system_sms_receive_status } = useIntDict(
  DICT_TYPE.system_sms_send_status,
  DICT_TYPE.system_sms_receive_status
)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<SmsLogVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  smsLogId: undefined,
  channelId: undefined,
  templateCode: undefined,
  templateType: undefined,
  apiTemplateId: undefined,
  mobile: undefined,
  sendStatus: undefined,
  sendTime: [],
  receiveStatus: undefined,
  receiveTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const channelList = ref<SmsChannelVO[]>([]) // 短信渠道列表

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await SmsLogApi.getSmsLogPage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 详情 */
const detailRef = ref<InstanceType<typeof SmsLogDetail>>()
const openDetail = (data: SmsLogVO) => {
  detailRef.value?.open(data)
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  channelList.value = await SmsChannelApi.getSmsChannelSimpleList()
})
</script>

<style scoped lang="scss"></style>
